class_name HAbilityRayCast
extends HAbility

@export var ray_cast_3d:RayCast3D
@export var own_body_3d:PhysicsBody3D

func _ready() -> void:
	init_ray_cast_3d()

func init_ray_cast_3d():
	ray_cast_3d.add_exception(own_body_3d)


func execute():
	var obj = ray_cast_3d.get_collider()
	if is_instance_valid(obj):
		if obj is Node:
			var entity:Entity = HNodeUtilties.get_node_entity(obj)
			if not entity:
				return
			var comp:PhysicalClickComp = get_physic_click_comp(entity)
			if not comp:
				return
			comp.click_func()

func physic_execute():
	var obj = ray_cast_3d.get_collider()
	if is_instance_valid(obj):
		if obj is Node:
			var entity:Entity = HNodeUtilties.get_node_entity(obj)
			if not entity:
				return
			var comp:PhysicalClickComp = get_physic_click_comp(entity)
			if not comp:
				return
			comp.hover_func()

func get_physic_click_comp(entity:Entity):
	for comp in entity.componet_list:
		if comp is PhysicalClickComp:
			return comp
	return null
